<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$keyname = preg_replace('/[^a-zA-Z0-9]/', '', get('js'));

$THEME->addBreadcrumb($this->adminUrl.'/edit-javascript/?js='.$keyname, $this->lang('edit_javascript'));
$THEME->addBreadcrumb($this->adminUrl.'/edit-javascript/?js='.$keyname, $this->getContentLang('title', $keyname));

$mData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxjs_javascripts WHERE id = '{$keyname}'");



if($mData===false){
    JFX::addError($this->lang('invalid_keyname'));
}else{

    if(Antz_IntelliForm::submitted('edit-js')){
        $error = false;

        $keyname = preg_replace('/[^a-zA-Z0-9]/', '', post('keyname'));
        $title = post('title');
        $js = $_POST['js'];
        $default = (int) post('default');
        $version = post('version');

        if($version == 'v_all') $version = '';

        if(trim($keyname)==''){
            $error = true;
            JFX::addError($this->lang('invalid_keyname'));
        }


        if($keyname != $mData['keyname'] && $DB->countRows($CONFIG->dbprefix.'jfxjs_javascripts', "id = '{$keyname}'")>0){
            $error = true;
            JFX::addError($this->lang('keyname_already_registered'));
            JFX::addFormError('keyname');
        }

		if(!$error){
			$this->setFile($keyname, $js, $version);
			$this->updateContentLang('title', $keyname, $title, '*', '*');
			JFX::addSuccess($this->lang('success'));
		}

    }

$mData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxjs_javascripts WHERE id = '{$keyname}'");





$versionOpts = array('v_all'=>$this->lang('v_all'));
foreach($this->browserVersions as $k=>$v){
    $versionOpts[$v] = $this->lang('v_'.$v);
}


    $formFields = array(
        array(
            'type' => 'hidden',
            'name' => 'keyname',
            'value' => post('keyname', $mData['id']),
            'label' => $this->lang('keyname')
        ),
        array(
            'type' => 'text',
            'name' => 'title',
            'value' => post('title', $this->getContentLang('title', $mData['id'])),
            'label' => $this->lang('title')
        ),
        array(
            'type' => 'checkbox',
            'name' => 'default',
            'value' => '1',
            'checked' => (bool) post('default', $mData['is_default']),
            'label' => $this->lang('default_for_new_pages')
        ),
        array(
            'type' => 'select',
            'name' => 'version',
            'value' => post('version', $mData['browser_version']),
            'options' => $versionOpts,
            'label' => $this->lang('browser_version')
        ),
        array(
            'type' => 'code',
            'id' => 'jfxmenunew',
            'class' => 'javascript',
            'name' => 'js',
            'value' => post('js', file_get_contents($this->dirname.'/files/'.$mData['id'].'.js'), 500000),
            'label' => $this->lang('js')
        ),
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'value' => $this->lang('submit'),
            'label' => '&nbsp;'
        )
    );






    echo JFX::makeRapidForm($formFields, $this->lang('edit_legend'), 'edit-js');

	echo '<script type="text/javascript" language="JavaScript">CodePress.run();</script>';

}

